package cn.jobstart.service.impl;

import cn.jobstart.common.StringUtil;
import cn.jobstart.dao.IAdminJobCandidateDao;
import cn.jobstart.es.model.ResumeEs;
import cn.jobstart.es.search.ResumeEsSearch;
import cn.jobstart.es.service.IResumeEsService;
import cn.jobstart.modelExt.AdminJobCandidateExt;
import cn.jobstart.pubInter.AbstractBaseService;
import cn.jobstart.pubInter.IBaseDao;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.searchExt.AdminJobCandidateSearchExt;
import cn.jobstart.service.IAdminJobCandidateService;
import cn.jobstart.service.IAdminJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * @author sunke
 * @Date 2023-09-06 15:00:20
 * @description AdminJobCandidateService      职位候选人
 */
@Service(value = "adminJobCandidateService")
@Slf4j
public class AdminJobCandidateServiceImpl extends AbstractBaseService<AdminJobCandidateExt, AdminJobCandidateSearchExt, String> implements IAdminJobCandidateService {

    @Resource(name = "adminJobCandidateDao")
    private IAdminJobCandidateDao adminJobCandidateDao;

    @Resource
    private IResumeEsService resumeEsService;


    @Resource
    private IAdminJobService adminJobService;

    @Override
    public IBaseDao<AdminJobCandidateExt, AdminJobCandidateSearchExt, String> getDao() {
        return adminJobCandidateDao;
    }


    @Override
    public void afterInsert(AdminJobCandidateExt obj) throws SkException {
        super.afterInsert(obj);
        adminJobService.updateCandidateTotalByAdminJobIdAndCount(obj.getAdminJobId(), 1);
    }

    @Override
    public void afterDelete(String id) throws SkException {
        super.afterDelete(id);
    }

    /**
     * 找到某个员工职位ID 对应的所有候选简历IDs
     *
     * @param adminJobId
     * @return
     */
    @Override
    public List<String> getResumeIdsByAdminJobId(String adminJobId) {
        if (StringUtil.isNullOrEmpty(adminJobId)) {
            return null;
        }

        try {
            List<AdminJobCandidateExt> list = getAllList(AdminJobCandidateSearchExt.getInstance().setAdminJobIdEq(adminJobId));
            if (StringUtil.isNullOrEmpty(list)) {
                return null;
            }
            return StringUtil.removeNullAndDistinct(list.stream().map(AdminJobCandidateExt::getResumeId).distinct().collect(Collectors.toList()));


        } catch (SkException e) {

        }
        return null;
    }

    /**
     * 根据简历和员工ID 查询
     *
     * @param resumeId
     * @param adminJobId
     * @return
     */
    @Override
    public AdminJobCandidateExt getDetailByResumeIdAndAdminJobId(String resumeId, String adminJobId) {
        if (StringUtil.isNullOrEmpty(resumeId) || StringUtil.isNullOrEmpty(adminJobId)) {
            return null;
        }

        return getDetailBySearch(AdminJobCandidateSearchExt.getInstance().setAdminJobIdEq(adminJobId).setResumeIdEq(resumeId));
    }

    @Override
    public void fullOtherData(List<AdminJobCandidateExt> list, Set<String> fullConfigSet) {
        super.fullOtherData(list, fullConfigSet);
        fullResumeEs(list, fullConfigSet);
    }

    public void fullResumeEs(List<AdminJobCandidateExt> list, Set<String> fullConfigSet) {
        try {
            if (StringUtil.isNullOrEmpty(list)) {
                return;
            }

            List<String> resumeIds = list.stream().map(AdminJobCandidateExt::getResumeId).distinct().collect(Collectors.toList());

            resumeIds = StringUtil.removeNullAndDistinct(resumeIds);
            if (StringUtil.isNullOrEmpty(resumeIds)) {
                return;
            }

            ResumeEsSearch resumeEsSearch = ResumeEsSearch.getInstance();
            resumeEsSearch.setIds(resumeIds);


            List<ResumeEs> resumeEsList = resumeEsService.selectList(resumeEsSearch);
            if (StringUtil.isNullOrEmpty(resumeEsList)) {
                return;
            }

            list.stream().forEach(item -> {

                for (ResumeEs resumeEs : resumeEsList) {
                    if (item.getResumeId().equalsIgnoreCase(resumeEs.getId())) {
                        item.setResumeEs(resumeEs);
                        break;
                    }

                }


            });


        } catch (SkException e) {

        }


    }


}